﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CI.Models;
using CI.Models.Data;
using CI.Models.Production;
using CI.Models.Production.Interfaces;
using QuickGraph;

namespace CI.UI.Prototypes
{
    public class EveMapViewModel : ModelBase
    {
        private IStaticDataRepository<Region> DataRepository { get; set; }
        private BidirectionalGraph<object, IEdge<object>> _regionGraph;
        public BidirectionalGraph<object,IEdge<object>> RegionGraph { get { return _regionGraph;} private set { SetField(ref _regionGraph, value, () => RegionGraph); }}

        public IEnumerable<dynamic> RegionData { get { return DataRepository.All; } }

        public string[] LayoutAlgorithms
        {
            get
            {
                return new[]
                       {
                           "Circular", "Tree", "FR", "BoundedFR", "KK", "ISOM", "LinLog",
                           "CompoundFDP"
                       };
            }
        }

            public EveMapViewModel(IStaticDataRepository<Region> dataRepository)
        {
            DataRepository = dataRepository;

            // build the region graph!!

            var graph = new BidirectionalGraph<object, IEdge<object>>(true,DataRepository.All.Count());
                /*
            foreach(var region in DataRepository.All)
            {
                if (region.JumpFrom!=null || region.JumpTo!=null)CheckAndAddVertex(graph, (object) region);
                if (region.JumpFrom!=null)
                    foreach(var jumpFrom in region.JumpFrom)
                    {
                        var targetRegion = (object) DataRepository.Get(jumpFrom.toRegionID);
                        CheckAndAddVertex(graph, targetRegion);
                        CheckAndAddEdge(graph, region, targetRegion);
                    }

                RegionGraph = graph;
            }*/
        }

        private static void CheckAndAddEdge(BidirectionalGraph<object, IEdge<object>> graph, object sourceRegion, object targetRegion)
        {
            IEdge<object> existing = null;

            if (!graph.TryGetEdge(sourceRegion,targetRegion,out existing))
            {
                graph.AddEdge(new Edge<object>(sourceRegion, targetRegion));
            }
        }

        private static void CheckAndAddVertex(BidirectionalGraph<object, IEdge<object>> graph, object region)
        {
            if (!graph.ContainsVertex(region))
            {
                graph.AddVertex(region);
            }
        }
    }
}
